//
//  YKQAlertView.m
//  YaoKongQi
//
//  Created by ryou on 2021/5/24.
//

#define kSpace 20.0

#define kItemWidth (kYKQWidth - kSpace * 6)/3
#define kBGWidth kItemWidth * 3 + kSpace * 4

#define kBGHeight kYKQHeight - 200.0

#import "YKQAlertView.h"
@interface YKQAlertView ()

@property(nonatomic,copy)void(^didSelectTag)(void);

@end
@implementation YKQAlertView

static YKQAlertView *_instance=nil;

+(YKQAlertView *)sharedInstance{
    
    if (_instance==nil)
    {
        _instance=[[super alloc]init];
    }
    return _instance;
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self==[super initWithFrame:frame]) {
        UIView *window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:self];
        self.frame=CGRectMake(0, 0, kYKQWidth, kYKQHeight);
        self.userInteractionEnabled = YES;
        self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    
    }

    return self;

}

-(void)showAlertWithWithTag:(void (^)(void))didSelectTag{
    _didSelectTag = didSelectTag;
    
    [UIView animateWithDuration:0.1 animations:^{

        self.backgroundColor =  [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
   
    } completion:^(BOOL finished) {
 
        CGFloat wwww1 = kYKQWidth;
        CGFloat wwww2 = kBGWidth;
        CGFloat wwww3 = wwww1-wwww2;
        
        CGFloat hhh1 = kYKQHeight;
        CGFloat hhh2 = kBGHeight;
        CGFloat hhh3 = hhh1-hhh2;
        UIView *bg=[[UIView alloc]initWithFrame:CGRectMake(wwww3/2,hhh3/2, kBGWidth , kBGHeight)];
        bg.backgroundColor=[UIColor colorWithRed:0.926 green:0.949 blue:0.976 alpha:1.0];
        bg.layer.cornerRadius = 10;
        [self addSubview:bg];
        
        UILabel *jiesuo=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, bg.frame.size.width, 50)];
        jiesuo.text = @"解锁此功能";
        jiesuo.textAlignment=NSTextAlignmentCenter;
        jiesuo.font = [UIFont boldSystemFontOfSize:25];
        jiesuo.textColor=[UIColor colorWithRed:0.896 green:0.409 blue:0.576 alpha:1.0];
        [bg addSubview:jiesuo];
        
        UILabel *jiesuotips=[[UILabel alloc]initWithFrame:CGRectMake(0,jiesuo.frame.size.height+ jiesuo.frame.origin.y +  10, bg.frame.size.width, 30)];
        jiesuotips.text = @"完整观看此视频（约30s）";
        jiesuotips.textAlignment=NSTextAlignmentCenter;
        jiesuotips.font = [UIFont boldSystemFontOfSize:15];
        jiesuotips.textColor=[UIColor colorWithRed:0.616 green:0.642 blue:0.694 alpha:1.0];
        [bg addSubview:jiesuotips];

        UIButton *savebtn=[[UIButton alloc]initWithFrame:CGRectMake(bg.frame.size.width / 2 - 70, bg.frame.size.height - 70, 140, 50)];
        [savebtn setBackgroundImage:[UIImage imageNamed:@"ykq_icon_white_bg.png"] forState:UIControlStateNormal];
        [bg addSubview:savebtn];
        savebtn.layer.cornerRadius = 25;
        savebtn.clipsToBounds=YES;
        [savebtn setTitle:@"永久解锁" forState:UIControlStateNormal];
        [savebtn setTitleColor:kRedColor forState:UIControlStateNormal];
        [savebtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self shakeToShow:savebtn];
        
        UIImageView *tipsimage=[[UIImageView alloc]initWithFrame:CGRectMake(20, jiesuotips.frame.size.height+jiesuotips.frame.origin.y+ 10, bg.frame.size.width - 40, bg.frame.size.height - 50 - 50- 30  - 10-10-20-10 - 10)];
        tipsimage.image=[UIImage imageNamed:@"ykq_bg.png"];
        tipsimage.contentMode=UIViewContentModeScaleAspectFit;
        [bg addSubview:tipsimage];
    }];

}
- (void) shakeToShow:(UIView *)aView
{
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.6;// 动画时间
    NSMutableArray *values = [NSMutableArray array];
    // 前两个是控制view的大小的；
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.repeatCount=MAXFLOAT;
    [aView.layer addAnimation:animation forKey:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self hidden];
}

-(void)hidden{
  
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
                
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        [self removeFromSuperview];
        _didSelectTag = nil;
        _instance = nil;
    }];
}
-(void)saveBtnClick{

    if(self.didSelectTag ){
        self.didSelectTag();
    }
    [self hidden];
}


@end
